/*
 * KitchenRestController.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.web.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.onesalt.domain.Dinner;
import com.onesalt.domain.Kitchen;
import com.onesalt.service.DinnerService;
import com.onesalt.service.KitchenService;
import com.onesalt.web.api.request.DeleteDishesRequest;
import com.onesalt.web.api.request.SyncDishesRequest;
import com.onesalt.web.api.response.KitchenResponse;
import com.onesalt.web.api.response.Response;

/**
 * 
 *
 * @author kiri
 * @date 2015年5月4日
 * @version 1.0
 */
@RestController
@RequestMapping("/api/kitchens")
public class KitchenRestController extends AbstractRestController {

	@Autowired
	private KitchenService kitchenService;
	
	@Autowired
	private DinnerService dinnerService;
	
	@RequestMapping(value = "/syncDishes", method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_VALUE })
	public KitchenResponse syncDishes(@RequestBody SyncDishesRequest request) {
		Kitchen kitchen = getKitchenService().syncDishes(request.getKitchenId(), request.getDishes());
		KitchenResponse response = new KitchenResponse();
		response.setKitchen(kitchen);
		return response;
	}

	@RequestMapping(value = "/deleteDishes", method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_VALUE })
	public Response deleteDishes(@RequestBody DeleteDishesRequest request) {
		getKitchenService().getDishesRepository().delete(request.getDishesId());
		return Response.SUCCESS;
	}
	
	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	public KitchenResponse getDetail(@RequestParam Long kitchenId) {
		Kitchen kitchen = getKitchenService().getKitchenRepository().findOne(kitchenId);
		Dinner dinner = getDinnerService().getDinnerRepository().findTopByKitchenIdOrderByStartTimeDesc(kitchenId);
		KitchenResponse response = new KitchenResponse();
		response.setKitchen(kitchen);
		response.setLastDinner(dinner);
		return response;
	}
	
	@RequestMapping(value = "/detailByUser", method = RequestMethod.GET)
	public KitchenResponse getDetailByUser(@RequestParam Long userId) {
		Kitchen kitchen = getUserService().getKitchenRepository().findTopByCookId(userId);
		Dinner dinner = getDinnerService().getDinnerRepository().findTopByKitchenIdOrderByStartTimeDesc(kitchen.getKitchenId());
		KitchenResponse response = new KitchenResponse();
		response.setKitchen(kitchen);
		response.setLastDinner(dinner);
		return response;
	}
	
	public KitchenService getKitchenService() {
		return kitchenService;
	}

	public void setKitchenService(KitchenService kitchenService) {
		this.kitchenService = kitchenService;
	}

	public DinnerService getDinnerService() {
		return dinnerService;
	}

	public void setDinnerService(DinnerService dinnerService) {
		this.dinnerService = dinnerService;
	}
}
